📌 CREATE TABLE `list`
(
id int AUTO_INCREMENT PRIMARY KEY,
ip varchar(255) not null,
time varchar(255) not null
);
📌 require_once("./lib/database.php");
require_once("./lib/http.php");
$sql = new sql();
$http = new http();
📌 public function check($ip)
{
$check = false;
$pdo = $this->conn();
$sql = "SELECT * FROM `". $this->db ."`";
$stmt = $pdo->prepare($sql);
$stmt->execute();
try
{
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if($row[$this->field[1]] == $ip)
{
$check = true;
}
}
}
catch (PDOException $e)
{
die();
}
unset($pdo);
return $check;
}
📌 class http
{
public $ip;
public function client_ip( )
{
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$this->ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$this->ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (!empty($_SERVER["REMOTE_ADDR"]))
{
$this->ip = $_SERVER["REMOTE_ADDR"];
}
else
{
$this->ip = "NULL";
}
if ($this->ip === "::1")
{
$this->ip = '127.0.0.1';
}
return $this->ip;
}
public function time()
{
return date('Y-m-d-H-i-s');
}
}
📌 f($sql->check($http->client_ip()))
{
http_response_code(404);
echo $sql->check($http->client_ip());
echo require "./views/error.php";
die();
$txt -> put_test("嘗試進入");
$txt -> write();
}
當獲取到的使用者IP,為資料庫所記錄的
回傳404錯誤,阻擋進入網站
程式碼收錄:https://github.com/chyhhwen/shopping-system